package TUILayer;
import java.util.Scanner;
import CTRLayer.OrderCtr;

/**
 * A class to manage the interaction between the user and the order controller
 * 
 * @author Mike M. Madsen, Rasmus A. Borup
 * @version 15/12-2013
 */
public class OrderTUI
{
    // Instance variables
    private String searchEAN;
    private String orderID;
    private OrderCtr orderCtr;
    private Scanner keyboard = new Scanner(System.in);
    private double totalPrice;
    
    /**
     * Constructor for objects of class OrderTUI
     */
    public OrderTUI()
    {
        orderCtr = new OrderCtr();
    }
    
    /**
     * Starts the order process
     */
    public void start()
    {
        orderMenu();
    }
    
    /**
     * Calls the writeOrderMenu method and depending on the returnvalue
     * starts a part of the system. Can make a call to the newOrder method, 
     * remove an order from the system or end the program.
     */
    private void orderMenu()
    {
        boolean exit = false;
        while (!exit)
        {
            int choice = writeOrderMenu();
            if (choice == 1)
            {
                newOrder(inputCustomerType(), inputCustomerID(), inputWantedDelivery());
            }
            else if (choice == 2)
            {
                orderCtr.removeOrder(inputOrderID());
            }
            else if(choice == 3)
            {
                exit = true;
            }
            
        }
    }
    
    /**
     * Calls the writeOrderManager method and depending on the returnvalue
     * will add a partorder to an order or return to the orderMenu
     */
    private void orderManager()
    {  
        boolean exitOrderManager = false;
        while (!exitOrderManager)
        {
            int choice = writeOrderManager();
            if (choice == 1)
            {
                addPartOrder(inputPartOrderAmount(), inputPartOrderEAN());
                totalPrice = orderCtr.totalOrderPrice(orderID);
            }
            else if (choice == 2)
            {
                
            }
            else if(choice == 3)
            {
                exitOrderManager = true;
            }
            
        }
    }
    
    /**
     * Prints out the order manager used after creating a new order
     * Asks the user to choose an option and returns the choice
     */
    private int writeOrderManager()
    {       
            orderCtr.getOrderDetails(orderID);
            System.out.println("Total price: " + totalPrice);
            System.out.println("\n\n*** Order Manager ***");
            System.out.println("(1) Add products");
            System.out.println("(2) Remove products from order(Not implemented yet)");
            System.out.println("(3) Save order");
            System.out.println("*********************\n \n");
            System.out.print("\n Make your choice: ");
  
            return keyboard.nextInt();
    }
    
    /**
     * Prints out the order menu usen when starting the orderTUI.
     * Asks the user to choose an option and returns the choice
     */
    private int writeOrderMenu()
    {       
            System.out.println("\f *** Main Menu ***");
            System.out.println(" (1) Create new order");
            System.out.println(" (2) Delete orders");
            System.out.println(" (3) Close");
            System.out.print("\n Make your choice: ");

            return keyboard.nextInt();
        }
    
    /**
     * Creates a new order object and adds a customer. If a customer
     * isn't found prints an error message and returns to the order menu
     */
    private void newOrder(String customerType, String customerID, boolean wantedDelivery)
    {
        if(orderCtr.getCorporateCustomerCtr().getCorporateCustomer(customerID) != null)
        {
            orderID = orderCtr.addOrder(customerType, customerID, wantedDelivery);
            orderManager();
        }
        else
        {
            System.out.println("Customer wasn't found.");
            try
            {
                Thread.sleep(2000);
            }
            catch(InterruptedException ex)
            {
                Thread.currentThread().interrupt();
                writeOrderMenu();
            }
        }
    }
    
    /**
     * Creates and adds a partorder to the last created order. If the 
     * product for the partorder isn't found prints an error message and 
     * returns to the order manager.
     */
    private void addPartOrder(double amount, String EAN)
    {
        if(orderCtr.getproductCtr().findProductByEAN(EAN) != null)
        {
            orderCtr.createPartOrder(amount, EAN, orderID);
        }
        else
        {
            System.out.println("Product wasn't found.");
            try
            {
                Thread.sleep(2000);
            }
            catch(InterruptedException ex)
            {
                Thread.currentThread().interrupt();
                writeOrderManager();
            }
        }
    }
    
    /**
     * Removes an order from the system determined by orderID. If the order
     * isn't found prints an error message and returns to the order menu
     */
    private void removeOrder(String orderID)
    {
        if(orderCtr.getOrder(orderID) != null)
        {
            orderCtr.removeOrder(orderID);
        }
        else
        {
            System.out.println("Order wasn't found.");
            try
            {
                Thread.sleep(2000);
            }
            catch(InterruptedException ex)
            {
                Thread.currentThread().interrupt();
                writeOrderMenu();
            }
        }
    }
    
    /**
     * Returns a string containing the users input when prompted for Customer type
     */
    private String inputCustomerType()
    {
        System.out.print("Costumer type: ");
        String inputType = keyboard.next();
        return inputType;
    }

    /**
     * Returns a string containing the users input when prompted for Customer ID
     */
    private String inputCustomerID()
    {
        System.out.print("Costumer ID: ");
        String inputID = keyboard.next();
        return inputID;
    }
    
    /**
     * Returns a string containing the users input when prompted for Order ID
     */
    private String inputOrderID()
    {
        System.out.print("Order ID: ");
        String inputOrderID = keyboard.next();
        return inputOrderID;
    }
    
    /**
     * Returns a double containing the users input when prompted for Amount
     */
    private double inputPartOrderAmount()
    {
        System.out.print("Amount:");
        double inputPartOrderAmount = keyboard.nextDouble();
        return inputPartOrderAmount;
    }
    
    /**
     * Returns a string containing the users input when prompted for EAN
     */
    private String inputPartOrderEAN()
    {
        System.out.print("EAN:");
        searchEAN = keyboard.next();
        return searchEAN;
    }
    
    /**
     * Returns a string containing the users input when prompted for Wanted delivery
     */
    private boolean inputWantedDelivery() 
    {
        boolean inputWantedDelivery = false;
        Character yn;
        do 
        {
            System.out.print("Wanted delivery?? enter Y/N: ");
            yn = (keyboard.next()).charAt(0);
            System.out.println("" + yn);
        } 
        while (!(yn == 'Y' || yn == 'y' || yn == 'N' || yn == 'n'));
        if (yn == 'Y' || yn == 'y')
        {
            inputWantedDelivery = true;
        }
        else if (yn == 'N' || yn == 'n')
        {
            inputWantedDelivery = false;
        }
        return inputWantedDelivery;
    }
}
